home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / NT / CODE / CHAP05 / CTLDEMO4 / CTLDEMO4.CPP next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  3.9 KB  |  125 lines

  1. //***********************************************************************
  2. //
  3. //  CtlDemo4.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "CtlDemo4.h"
  9.  
  10. #define IDC_RED     100
  11. #define IDC_GREEN   101
  12. #define IDC_BLUE    102
  13.  
  14. CMyApp myApp;
  15.  
  16. /////////////////////////////////////////////////////////////////////////
  17. // CMyApp member functions
  18.  
  19. BOOL CMyApp::InitInstance ()
  20. {
  21.     m_pMainWnd = new CMainWindow;
  22.     m_pMainWnd->ShowWindow (m_nCmdShow);
  23.     m_pMainWnd->UpdateWindow ();
  24.     return TRUE;
  25. }
  26.  
  27. /////////////////////////////////////////////////////////////////////////
  28. // CMainWindow message map and member functions
  29.  
  30. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  31.     ON_WM_CREATE ()
  32.     ON_WM_CTLCOLOR ()
  33.     ON_BN_CLICKED (IDC_RED, OnButtonClicked)
  34.     ON_BN_CLICKED (IDC_GREEN, OnButtonClicked)
  35.     ON_BN_CLICKED (IDC_BLUE, OnButtonClicked)
  36. END_MESSAGE_MAP ()
  37.  
  38. CMainWindow::CMainWindow ()
  39. {
  40.     CString strWndClass = AfxRegisterWndClass (
  41.         0,
  42.         myApp.LoadStandardCursor (IDC_ARROW),
  43.         (HBRUSH) (COLOR_3DFACE + 1),
  44.         myApp.LoadStandardIcon (IDI_APPLICATION)
  45.     );
  46.  
  47.     Create (strWndClass, "CtlDemo4");
  48. }
  49.  
  50. int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
  51. {
  52.     if (CFrameWnd::OnCreate (lpcs) == -1)
  53.         return -1;
  54.  
  55.     CClientDC dc (this);
  56.     int nHeight = -((dc.GetDeviceCaps (LOGPIXELSY) * 8) / 72);
  57.  
  58.     m_font.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
  59.         DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  60.         DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
  61.  
  62.     CFont* pOldFont = dc.SelectObject (&m_font);
  63.     TEXTMETRIC tm;
  64.     dc.GetTextMetrics (&tm);
  65.     m_cxChar = tm.tmAveCharWidth;
  66.     m_cyChar = tm.tmHeight + tm.tmExternalLeading;
  67.     dc.SelectObject (pOldFont);
  68.  
  69.     m_ctlGroupBox1.Create ("Sample text", WS_CHILD | WS_VISIBLE |
  70.         BS_GROUPBOX, CRect (m_cxChar * 2, m_cyChar, m_cxChar * 62,
  71.         m_cyChar * 8), this, UINT (-1));
  72.  
  73.     m_ctlText.Create ("Click a button to change my color",
  74.         WS_CHILD | WS_VISIBLE | SS_CENTER, CRect (m_cxChar * 4,
  75.         m_cyChar * 4, m_cxChar * 60, m_cyChar * 6), this);
  76.  
  77.     m_ctlGroupBox2.Create ("Color", WS_CHILD | WS_VISIBLE |
  78.         BS_GROUPBOX, CRect (m_cxChar * 64, m_cyChar, m_cxChar * 80,
  79.         m_cyChar * 8), this, UINT (-1));
  80.  
  81.     m_ctlRadioButtonRed.Create ("Red", WS_CHILD | WS_VISIBLE | WS_GROUP |
  82.         BS_AUTORADIOBUTTON, CRect (m_cxChar * 66, m_cyChar * 3,
  83.         m_cxChar * 78, m_cyChar * 4), this, IDC_RED);
  84.  
  85.     m_ctlRadioButtonGreen.Create ("Green", WS_CHILD | WS_VISIBLE |
  86.         BS_AUTORADIOBUTTON, CRect (m_cxChar * 66, (m_cyChar * 9) / 2,
  87.         m_cxChar * 78, (m_cyChar * 11) / 2), this, IDC_GREEN);
  88.  
  89.     m_ctlRadioButtonBlue.Create ("Blue", WS_CHILD | WS_VISIBLE |
  90.         BS_AUTORADIOBUTTON, CRect (m_cxChar * 66, m_cyChar * 6,
  91.         m_cxChar * 78, m_cyChar * 7), this, IDC_BLUE);
  92.  
  93.     m_ctlRadioButtonRed.SetCheck (1);
  94.  
  95.     m_ctlGroupBox1.SetFont (&m_font, FALSE);
  96.     m_ctlGroupBox2.SetFont (&m_font, FALSE);
  97.     m_ctlRadioButtonRed.SetFont (&m_font, FALSE);
  98.     m_ctlRadioButtonGreen.SetFont (&m_font, FALSE);
  99.     m_ctlRadioButtonBlue.SetFont (&m_font, FALSE);
  100.     return 0;
  101. }
  102.  
  103. HBRUSH CMainWindow::OnCtlColor (CDC* pDC, CWnd* pWnd, UINT nCtlColor)
  104. {
  105.     if (pWnd->m_hWnd == m_ctlText.m_hWnd) {
  106.         COLORREF crColor;
  107.         if (m_ctlRadioButtonRed.GetCheck ())
  108.             crColor = RGB (255, 0, 0);
  109.         else if (m_ctlRadioButtonGreen.GetCheck ())
  110.             crColor = RGB (0, 255, 0);
  111.         else
  112.             crColor = RGB (0, 0, 255);
  113.  
  114.         pDC->SetTextColor (crColor);
  115.         pDC->SetBkColor (::GetSysColor (COLOR_3DFACE));
  116.         return ::GetSysColorBrush (COLOR_3DFACE);
  117.     }
  118.     return CFrameWnd::OnCtlColor (pDC, pWnd, nCtlColor);
  119. }
  120.  
  121. void CMainWindow::OnButtonClicked ()
  122. {
  123.     m_ctlText.Invalidate ();
  124. }
  125.